package com.xuzhijie.pms.config;

import com.xuzhijie.pms.config.interceptor.ErrorInterceptor;
import com.xuzhijie.pms.config.interceptor.JWTInterceptors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(jwtInterceptors())
               .addPathPatterns("/**")
               .excludePathPatterns("/**/login")
               .excludePathPatterns("/owner/**")
               .excludePathPatterns("/image/**")
               .excludePathPatterns("/upload-images")
               .excludePathPatterns("/**/upload-images")
               .excludePathPatterns("/**/register")
               .excludePathPatterns("/file/**")
               .excludePathPatterns("/file/importProperty");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("authorization", "content-type")
                .maxAge(3600);
    }
    @Bean
    public JWTInterceptors jwtInterceptors(){
        return new JWTInterceptors();
    }
}
